package businessServlets;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.taskdefs.Sleep;
import org.omg.CORBA.portable.Streamable;

import sessionMenegment.SessionMgr;
import validate.Validate;


import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.PhoneNumber;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.sun.xml.internal.ws.developer.MemberSubmissionAddressing.Validation;


import controller.Controller;


@SuppressWarnings("serial")
public class AddAdBusiness extends HttpServlet {
	
	ImagesService imagesService = ImagesServiceFactory.getImagesService(); 
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("sign in get =)");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		System.out.println(isMultipart);
		
		System.out.println("add ad!!!!!!!!! ");
		if (isMultipart)
		{
			
			
			try { 
				
				HashMap<String, String> formFields = new HashMap<String, String>();
				
				System.out.println("multi");
				ServletFileUpload upload = new ServletFileUpload();       
				//resp.setContentType("text/plain");
				
				FileItemIterator iterator = upload.getItemIterator(req); 
				
				String name;
				String value;
				byte[] imageBytes = null;
				
				while (iterator.hasNext()) {         
					FileItemStream item = iterator.next(); 
					InputStream stream = item.openStream();
				
					//string field
					if (item.isFormField()) {
						name = item.getFieldName();
						value = Streams.asString(stream);
						System.out.println("form field " + name + " value: "+value);
						formFields.put(name, value);
												
					}
					else //image
					{
						
						try
						{
							int len;           
							imageBytes = IOUtils.toByteArray(stream);
							
							if (imageBytes.length==0)
							{
								System.out.println("size 0 -> no image");
								imageBytes = null;
							}
							
							
							//resp.getOutputStream().write(newImage.getImageData(), 0, newImage.getImageData().length);
						}catch(Exception e)
						{
							e.printStackTrace();
							System.out.println("not image");
						}
						
					}
				} //end while
				
				
				String bizUserName = (String) req.getSession().getAttribute("userNameBusiness");
					
				System.out.println("username: "+bizUserName);
				
				int result = Controller.getInstance().addBizAd(bizUserName, imageBytes,formFields.get("link"));
					
				switch (result)
				{
				case 0:					
					resp.sendRedirect("myProfileBusiness2.jsp#advertisement");
					break;
				case 1:
					System.out.println("no image");					
					//resp.sendRedirect("home.jsp#joinbiz");
					break;
				case 2:
					System.out.println("no such biz");					
					//resp.sendRedirect("home.jsp#joinbiz");
					break;						
				}				
				
			} catch (Exception ex) {
				throw new ServletException(ex);     
			}
		} // end if
		
		
		//resp.sendRedirect("allOrders.jsp");
	} // end function
		


	
}
